const db = require('../db/index')
const createExcel = require('../js/exceljs')
const dayjs = require('dayjs')
// 获取轮播图列表
exports.shoppingList = (req, res) => {
  let pageData = req.body
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  let appid = pageData.appid
  const sql = `select * from shopping where appid=? and type=? ORDER BY sort ASC limit ${size} OFFSET  ${from} `
  db.query(sql, [appid, pageData.type], (err, res2) => {
    if (err) return res.cc(err)
    let sql2 = `select count(*) from shopping where appid=? and type=?`
    db.query(sql2, [appid, pageData.type], (err2, res3) => {
      if (err) return res.cc(err)
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })
  })
}
// 获取所有的商品
exports.shoppingAllList = (req, res) => {
  let pageData = req.body
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  let appid = pageData.appid
  const sql = `select * from shopping where appid=? and type in(?) ORDER BY sort ASC limit ${size} OFFSET  ${from} `
  db.query(sql, [appid, pageData.type], (err, res2) => {
    if (err) return res.cc(err)
    let sql2 = `select count(*) from shopping where appid=?  and type in(?)`
    db.query(sql2, [appid, pageData.type], (err2, res3) => {
      if (err) return res.cc(err)
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })
  })
}
// 添加商品
exports.shoppingListAdd = (req, res) => {
  let data = req.body

  if (data.introduceImg) {
    data.introduceImg = JSON.stringify(data.introduceImg)
  }
  let sql = `select * from shopping where appid=? and type=?`
  db.query(sql, [data.appid, data.type], (err, res3) => {
    if (err) return res.cc(err)
    data.sort = res3.length
    const sql2 = `insert into shopping set ?`
    db.query(sql2, [data], (err, res2) => {
      if (err) return res.cc(err)
      res.send({
        message: '添加成功'
      })
    })
  })
}
// 删除
exports.shoppingListDelete = (req, res) => {
  let data = req.body
  let sql = `delete from shopping where _id=?`
  db.query(sql, data.id, (err, res2) => {
    if (err) throw Error(err)
    res.send({
      message: '删除成功'
    })
  })
}
// 修改数据
exports.shoppingListChange = (req, res) => {
  let data = req.body
  if (data.introduceImg) {
    data.introduceImg = JSON.stringify(data.introduceImg)
  }
  let sql = `update shopping set ? where _id=?`
  db.query(sql, [data, data._id], (err, res2) => {
    if (err) return res.cc(err)
    res.send({
      message: '修改成功'
    })
  })
}
// 排序
exports.shoppingListSrot = (req, res) => {
  let data = req.body
  try {
    data.ids.forEach((item, index) => {
      const sql = `update shopping set sort= ? where _id = ${item}`
      db.query(sql, [data.sort[index]], (err, res2) => {
        if (err) throw Error(err)
      })
    })
    res.send({
      messge: '排序成功'
    })
  } catch (error) {
    return res.cc(error)
  }

}
// 发货商品列表
exports.commodityList = (req, res) => {
  let pageData = req.body
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  let appid = pageData.appid
  const sql = `select * from commodity where appid=? limit ${size} OFFSET  ${from} `
  db.query(sql, [appid], (err, res2) => {
    if (err) return res.cc(err)
    let sql2 = `select count(*) from commodity where appid=?`
    db.query(sql2, [appid], (err2, res3) => {
      if (err) return res.cc(err)
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })
  })
}
// 删除
exports.commodityListDelete = (req, res) => {
  let data = req.body
  let sql = `delete from commodity where _id=?`
  db.query(sql, data.id, (err, res2) => {
    if (err) throw Error(err)
    res.send({
      message: '删除成功'
    })
  })
}
// 点击发货
exports.logisticsNumberAdd = (req, res) => {
  let data = req.body
  let sql = `update  commodity set logisticsNumber=? where _id=?`
  db.query(sql, [data.logisticsNumber, data._id], (err, res2) => {
    if (err) throw Error(err)
    res.send({
      message: '发货成功'
    })
  })
}
// 发货搜索
exports.commoditySerach = (req, res) => {
  let pageData = req.body
  let page = pageData.page * 1 - 1
  let size = pageData.size * 1
  let from = page * size
  let appid = pageData.appid
  pageData.productName = pageData.productName ? pageData.productName : ''
  pageData.consignee = pageData.consignee ? pageData.consignee : ''
  pageData.buyAccount = pageData.buyAccount ? pageData.buyAccount : ''
  pageData.consigneePhone = pageData.consigneePhone ? pageData.consigneePhone : ''
  pageData.logisticsNumber = pageData.logisticsNumber ? pageData.logisticsNumber : ''
  pageData.status = pageData.status ? pageData.status : ''
  const sql = `select * from commodity where appid=${appid}  and (productName like "%${pageData.productName}%" and consignee like "%${pageData.consignee}%" and buyAccount like "%${pageData.buyAccount}%" and consigneePhone like "%${pageData.consigneePhone}%" and logisticsNumber like "%${pageData.logisticsNumber}%" and status like "%${pageData.status}%") limit ${size} OFFSET  ${from}`
  db.query(sql, [], (err, res2) => {
    let sql2 = `select count(*) from commodity where appid=? and (productName like "%${pageData.productName}%" and consignee like "%${pageData.consignee}%" and buyAccount like "%${pageData.buyAccount}%" and consigneePhone like "%${pageData.consigneePhone}%" and logisticsNumber like "%${pageData.logisticsNumber}%" and status like "%${pageData.status}%")`
    db.query(sql2, [appid], (err2, res3) => {
      if (err2) return res.cc(err2)
      res.send({
        res: res2,
        total: res3[0]['count(*)']
      })
    })

  })
}
// 导出发货表格
exports.commodityCreateExcel = (req, res) => {
  let data = req.body
  columns = [
    { header: '订单号', key: 'orderNumber' },
    { header: '商品图', key: '', width: 10, height: 10 },
    { header: '商品名', key: 'productName' },
    { header: '价格', key: 'price' },
    { header: '个数', key: 'number' },
    { header: '购买账号', key: 'buyAccount' },
    { header: '地址', key: 'address' },
    { header: '收货人', key: 'consignee' },
    { header: '收货人电话', key: 'consigneePhone' },
    { header: '物流编号', key: 'logisticsNumber' },
    { header: '状态', key: 'status' },
  ]
  let sql = ``
  if (data.id == 'all') {
    sql = 'select * from commodity where appid=?'
  } else {
    sql = 'select * from commodity where appid=? and _id in (?)'
  }
  db.query(sql, [data.appid, data.id], async (err, res2) => {
    res2.forEach((item) => {
      if (item.status == 0) {
        item.status = '待发货'
      } else if (item.status == 1) {
        item.status = '已发货'
      } else if (item.status == 2) {
        item.status = '已取消'
      } else if (item.status == 3) {
        item.status = '已退款'
      }
    })
    if (err) return res.cc(err)
    const buffer = await createExcel.createExcel(columns, res2)
    res.setHeader('Content-disposition', 'attachment; filename=file.xlsx')
    res.setHeader('Content-type', 'application/octet-stream')
    res.send(buffer)


  })

}